package cn.cxj.org.service.impl;

import cn.cxj.basic.service.impl.BaseServiceImpl;
import cn.cxj.basic.util.PageList;
import cn.cxj.org.domain.Department;
import cn.cxj.org.mapper.DepartmentMapper;
import cn.cxj.org.query.DepartmentQuery;
import cn.cxj.org.service.IDepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
@Service
@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
public class DepartmentServiceImpl extends BaseServiceImpl<Department> implements IDepartmentService {

    @Autowired
    private DepartmentMapper departmentMapper;

    @Override
    public void add(Department department) {
        departmentMapper.save(department);
        if (department.getParent() == null) {
            String path = "/" + department.getId();
            department.setDirPath(path);
        } else {
            Department parent = super.queryById(department.getParent().getId());
            department.setDirPath(parent.getDirPath() + "/" + department.getId());
        }
        update(department);
    }

    @Override
    public void update(Department department) {
        if (department.getParent() == null) {
            String path = "/" + department.getId();
            department.setDirPath(path);
        } else {
            Department parent = super.queryById(department.getParent().getId());
            department.setDirPath(parent.getDirPath() + "/" + department.getId());
        }
        super.update(department);
    }

    @Override
    public List<Department> findOneAndTwoLevelTree() {
        return departmentMapper.loadOneAndTwoLevelTree();
    }
}
